-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/5/8
-- @module:   load_tree
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class load_tree
local load_tree = {}
local this = load_tree

local json = import('bt/bt_plugin/json')
---@type behavior_tree
local behavior_tree = import('bt/bt_plugin/behavior3/behavior_tree')
local sys_ini_ctx = sys_ini_ctx
this.rootPath = ''
this.treeDic = {}

-- 设置根目录
function this.set_rootPath(path)
    this.rootPath = path
end

-- 读ini
function this.read_ini(path)
    if sys_ini_ctx then
        local str = sys_ini_ctx:get_string(path)
        local ret = json.decode(str)
        return ret
    end
    return nil
end

-- 读取配置
function this.read_config(path)
    local fullPath = this.rootPath .. [[tree_data\]] .. path
    --xxmsg(fullPath)
    local file, err = io.open(fullPath, 'r')
    assert(file, err)
    local str = file:read('*a')
    file:close()
    local ret = json.decode(str)
    return ret
end

-- 得到行为树
function this.get_tree(name, fileName, ctx, owner)
    if this.treeDic[name] then
        return this.treeDic[name]
    end
    local ret = behavior_tree.new(name, this.read_config(fileName), {
        ctx   = ctx,
        owner = owner,
    })
    this.treeDic[name] = ret
    return ret
end

-- 清除
function this.clear()
    this.treeDic = {}
end

return this